﻿using System;
using System.Reflection;
using System.Data;

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace TADF.Com.Alpha.Core.DB
{
    public class SessionFactory
    {
        private static ISessionFactory sessions;
        private static Configuration cfg;
        static readonly object padlock = new object();

        public SessionFactory()
        {
        }

        public static ISession OpenSession(string AssemblyName)
        {
            if (sessions == null)
            {
                lock (padlock)
                {
                    if (sessions == null)
                    {
                        BuildSessionFactory(AssemblyName);
                    }
                }
            }
            return sessions.OpenSession();
        }

        private static void BuildSessionFactory(string AssemblyName)
        {
            cfg = new Configuration();

            cfg.AddAssembly(AssemblyName);

            sessions = cfg.BuildSessionFactory();
        }
    }
}
